package wcg;

import ikelib.wintime;

import javax.swing.JPanel;

import java.awt.Font;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JTextArea;
import java.awt.Rectangle;
import java.awt.Dimension;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;

public class DialogIrigInfo extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jPanelCenter = null;
	private JTextArea jTextAreaTime = null;
	private JTextPane jTextPaneIrigOut = null;
	private JLabel jLabelTime = null;
	private JButton jButtonCalc = null;
	private JButton jButtonOK = null;
	private JComboBox jComboBoxType = null;
	String[] ColumnColor=null;

	public void setTime(String time){
		jTextAreaTime.setText(time);
	}
	/**
	 * @param owner
	 */
	public DialogIrigInfo(Frame owner) {
		super(owner);
		initialize();
		ColumnColor = new String[6];
		ColumnColor[0] = "style=\"background:#ffff99\""; 
		ColumnColor[1] = "style=\"background:#ffcc99\""; 
		ColumnColor[2] = "style=\"background:#ccff99\""; 
		ColumnColor[3] = "style=\"background:#ffffcc\""; 
		ColumnColor[4] = "style=\"background:#ccccff\""; 
		ColumnColor[5] = "style=\"background:#ccffcc\""; 
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(696, 202);
		this.setTitle("IRIG-H2 (for OBS)");  // Generated
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelCenter(), BorderLayout.CENTER);  // Generated
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanelCenter	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelCenter() {
		if (jPanelCenter == null) {
			jLabelTime = new JLabel();
			jLabelTime.setBounds(new Rectangle(21, 14, 48, 27));  // Generated
			jLabelTime.setText("Time");  // Generated
			jPanelCenter = new JPanel();
			jPanelCenter.setLayout(null);  // Generated
			jPanelCenter.add(getJTextAreaTime(), null);  // Generated
			jPanelCenter.add(getJTextAreaIrigOut(), null);  // Generated
			jPanelCenter.add(jLabelTime, null);  // Generated
			jPanelCenter.add(getJButtonCalc(), null);  // Generated
			jPanelCenter.add(getJButtonOK(), null);  // Generated
			jPanelCenter.add(getJComboBoxType(), null);
		}
		return jPanelCenter;
	}

	/**
	 * This method initializes jTextAreaTime	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextAreaTime() {
		if (jTextAreaTime == null) {
			jTextAreaTime = new JTextArea();
			jTextAreaTime.setText("2012/1/1 0:0:0");
			jTextAreaTime.setBounds(new Rectangle(76, 14, 165, 27));  // Generated
		}
		return jTextAreaTime;
	}

	/**
	 * This method initializes jTextAreaIrigOut	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextPane getJTextAreaIrigOut() {
		if (jTextPaneIrigOut == null) {
			jTextPaneIrigOut = new JTextPane();
			jTextPaneIrigOut.setBounds(new Rectangle(8, 51, 672, 105));  // Generated
			Font fnt = Font.getFont(Font.MONOSPACED);
			if(fnt != null){
				jTextPaneIrigOut.setFont(fnt);
			}
			jTextPaneIrigOut.setContentType("text/html");
		}
		return jTextPaneIrigOut;
	}

	/**
	 * This method initializes jButtonCalc	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCalc() {
		if (jButtonCalc == null) {
			jButtonCalc = new JButton();
			jButtonCalc.setBounds(new Rectangle(253, 11, 80, 29));  // Generated
			jButtonCalc.setText("Calc");  // Generated
			jButtonCalc.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					doCalc();
				}
			});
		}
		return jButtonCalc;
	}
	private void doCalc(){
		String s = jTextAreaTime.getText();
		if(s!=null) {
			String str = calcIrig(s);
			jTextPaneIrigOut.setText(str);
		}
	}
	/**
	 * byteのバイナリデータを表示用文字列に変換する
	 * @param c
	 * バイナリデータ
	 * @param first
	 * Refマーカー付きなので、下位の桁を1ビットシフトさせる
	 * @param half
	 * 下位1桁のみを変換する
	 * @return
	 */
	private String byteToString(byte c,boolean first,boolean half){
		String ret = new String();
		int width = 8;
		if(half){
			width=4;
		}
		for(int i=0;i<width;i++){
			if((c & (0x1<<i)) !=0){
				ret += "1";
			}
			else{
				ret += "0";
			}
			if(i==3){
				if(first) ret+=" ";
				else      ret+=" 0 ";
			}
		}
		return ret;
	}
	
	private String makeHtmlTable(String addmessage, String[] title,String[] data)
	{
		String ret=new String();
		ret = "<table  border=\"1\" cellspacing=\"2\" cellpadding=\"2\">";
		ret += "<caption>"+addmessage +"</caption>";
		ret += "<tr>";
		ret += "<td>";
		ret += "Pr";
		ret += "</td>";
		for(int i=0;i<title.length;i++){
			ret += "<td " + ColumnColor[i] + ">";
			ret += title[i];
			ret += "</td>";
			ret += "<td>";
			ret += String.format("P%d",i+1);
			ret += "</td>";
		}
		ret += "</tr>";
		ret += "<tr>";
		ret += "<td>";
		ret += "Pr";
		ret += "</td>";
		for(int i=0;i<data.length;i++){
			ret += "<td " + ColumnColor[i] + ">";
			ret += data[i];
			ret += "</td>";
			ret += "<td>";
			ret += String.format("P%d",i+1);
			ret += "</td>";
		}
		ret += "</tr>";
		ret += "</table>";
		return ret;
	
	}
	/**
	 * IRIG H002 形式で表示する
	 * @param wt
	 * @return
	 */
	private String calcIrigH2(wintime wt){
		String ret = new String();
		ret += "P";
		byte[] tm = wt.getBcd();
		
		
		String[] data = new String[6];
		String[] title = new String[6];
		
		data[0] = byteToString(tm[5],true,false);
		title[0] = "second";
		data[1] = byteToString(tm[4],false,false);
		title[1] = "minute";
		data[2] = byteToString(tm[3],false,false);
		title[2] = "hour";
		
		int days = wt.getDayOfYear();
		String dy = String.format("Days=%d<br>",days);

		int lo,mid,hi;
		hi = days /100;
		days -= hi*100;
		mid = days /10;
		days -= mid*10;
		lo = days % 10;
		
		data[3] = byteToString((byte)(mid  * 16 + lo),false,false);
		title[3] = "days";
		
		data[4] = byteToString((byte)(hi),false,true) + " 0000";
		title[4] = "days ----";

		data[5] = byteToString((byte)(0),false,false);
		title[5] = "----";

		return makeHtmlTable("IRIG H002 " + wt.fmt() + " " + dy,title,data);
	}
	/**
	 * IRIG OBS 形式で表示する
	 * @param wt
	 * @return
	 */
	private String calcIrigOBS(wintime wt){
		String ret = new String();
		
		byte[] tm = wt.getBcd();
		
		String[] data = new String[6];
		String[] title = new String[6];
		
		data[0] = byteToString(tm[5],true,false);
		title[0] = "second";
		data[1] = byteToString(tm[4],false,false);
		title[1] = "minute";
		data[2] = byteToString(tm[3],false,false);
		title[2] = "hour";
		data[3] = byteToString(tm[2],false,false);
		title[3] = "day";
		data[4] = byteToString(tm[1],false,false);
		title[4] = "month";
		data[5] = byteToString(tm[0],false,false);
		title[5] = "year";
		
		return makeHtmlTable("IRIG OBS " + wt.fmt(),title,data);
		
	}
	/**
	 * IRIGを計算する
	 * @param time
	 * @return
	 */
	private String calcIrig(String time){
		wintime wt = new wintime();
		wt.parseDaySec(time);
		
		switch(jComboBoxType.getSelectedIndex()){
		case 0:
			return calcIrigOBS(wt);
		case 1:
			return calcIrigH2(wt);
		}
		return null;
	}
	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setBounds(new Rectangle(519, 13, 80, 28));  // Generated
			jButtonOK.setText("CLOSE");  // Generated
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
				}
			});
		}
		return jButtonOK;
	}
	/**
	 * This method initializes jComboBoxType	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxType() {
		if (jComboBoxType == null) {
			jComboBoxType = new JComboBox();
			jComboBoxType.setBounds(new Rectangle(400, 14, 103, 27));
			String[] mes = {"IRIG OBS","IRIG H002"};
			ComboBoxModel model = new DefaultComboBoxModel(mes); //@
			jComboBoxType.setModel(model);
			jComboBoxType.setSelectedIndex(0);
			jComboBoxType.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					doCalc();
				}
			});
		}
		return jComboBoxType;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
